<!--
 * @Autor: curry
 * @Description: 
 * @Date: 2022-11-28 17:55:04
-->
<template>
  <div class="com-container">
    <div class="com-chart" ref="rank_ref"></div>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  name: "Rank",

  data() {
    return {
      chartInstance: null,
      allData: null,
      startValue: 0, //区域缩放起点
      endValue: 9, //区域缩放终点
      timeId: null,
    };
  },
    computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme(){
      this.chartInstance.dispose()
      this.initChart();
      this.screenAdapter()
      this.updateCharts()
    }
  },

  created () {
    this.$socket.registerCallBack('rankData', this.getData);
  },

  mounted() {
    this.initChart();
    // this.getData();
    this.$socket.send({
      action: 'getData',
      socketType: 'rankData',
      chartName: 'rank',
      value: ''
    });
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    clearInterval(this.timeId);
  },

  methods: {
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.rank_ref, this.theme);
      const initOption = {
        title: {
          text: '▎ '+"地区销售排行",
          left: 20,
          top: 20,
        },
        grid: {
          top: "40%",
          left: "5%",
          right: "5%",
          left: "5%",
          containLable: true,
        },
        tooltip: {
          show: true,
        },
        xAxis: {
          type: "category",
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            type: "bar",
          },
        ],
      };
      this.chartInstance.setOption(initOption);
      this.chartInstance.on("mouseover", () => {
        clearInterval(this.timeId);
      });
      this.chartInstance.on("mouseout", () => {
        this.startSetInterVal();
      });
    },
    getData(res) {
      // let res = await this.$http.get("rank");
      this.allData = res;
      this.allData.sort((a, b) => {
        return b.value - a.value;
      });
      this.updateChart();
      this.startSetInterVal();
    },
    updateChart() {
      const colorArr = [
        ["#0ba82c", "#4ff778"],
        ["#2e72bf", "#23e5e5"],
        ["#5052ee", "#ab6ee5"],
      ];
      let privinceArr = this.allData.map((item) => {
        return item.name;
      });
      let valueArr = this.allData.map((item) => {
        return item.value;
      });
      const updateOption = {
        xAxis: {
          data: privinceArr,
        },
        series: [
          {
            data: valueArr,
            itemStyle: {
              color: (arg) => {
                let targetColor = null;
                if (arg.value > 300) {
                  targetColor = colorArr[0];
                } else if (arg.value > 200) {
                  targetColor = colorArr[1];
                } else {
                  targetColor = colorArr[2];
                }
                return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: targetColor[0],
                  },
                  {
                    offset: 1,
                    color: targetColor[1],
                  },
                ]);
              },
            },
          },
        ],
        dataZoom: {
          show: false,
          startValue: this.startValue,
          endValue: this.endValue,
        },
      };
      this.chartInstance.setOption(updateOption);
    },

    // 分辨率变化
    screenAdapter() {
      // 计算当前字体大小
      const titleFontSize = (this.$refs.rank_ref.offsetWidth / 100) * 3.6;
      const uptateOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
        series:{
          barWidth:titleFontSize,
          itemStyle:{
            barBorderRadius:[titleFontSize/2,titleFontSize/2,0,0]
          }
        },
        legend: {
          itemWidth: titleFontSize / 2,
          itemHeight: titleFontSize / 2,
          itemGap: titleFontSize / 2,
          textStyle: {
            fontSize: titleFontSize / 2,
          },
        },
      };
      this.chartInstance.setOption(uptateOption);
      this.chartInstance.resize();
    },
    startSetInterVal() {
      if (this.timeId) {
        clearInterval(this.timeId);
      }
      this.timeId = setInterval(() => {
        this.startValue++;
        this.endValue++;
        if (this.endValue > this.allData.length - 1) {
          this.startValue = 0;
          this.endValue = 9;
        }
        this.updateChart();
      }, 2000);
    },
  },
};
</script>

<style lang="scss" scoped></style>
